<?php
class admin_newscate extends CI_Controller {
	protected $_page_name = "admin_newscate";
	protected $_page_id = 'ul_postcontainer';
	protected $data; //data for this page.
	protected $submitdata; //data submit.
	protected $_page_title = ADMINTITLE_NEWSCATE;
	protected $_base_url;
	
	function  __construct()  {
		$this->_initdata();
	}
	
	//Controller: default
	function index() {
		//1. get danh muc tin (news_cate)
		$this->data['aNewsCate'] = $this->index_get_news_cate_list();
		//2. reset message
		$this->data['msg'] = $_SESSION['msg'];
		$_SESSION['msg'] = "";
		//3. show to browser.
		$this->showadmin($this->data);
	}		
	
	//Controller: edit
	function edit()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$id =  $_POST['h_id'];
			if ($id != "")
			{
				$this->submitdata['cate_name'] =  $_POST['txt_cate_name'];
				$this->submitdata['url'] =  convertToVn(trim($_POST['txt_cate_name']));
				$this->submitdata['status'] =  $_POST['cbo_Status'];									
				//update DB
				$this->Adminmodel->_update_newscate('news_cate', "cate_id", $_POST['h_id'], $this->submitdata);
				//write log	
				$this->Adminmodel->_write_log("Action,Step,Info", 
																			"update news,update record,newsid=".$id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
			}
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã cập nhật ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_newscate/1'."'</script>";				
		}		
		else
		{
			$this->data["page_name"]="admin_newscate_edit";
			$this->data['newscate_detail'] = $this->Adminmodel->_get_newscate_detail($this->uri->segment(3));	
			$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();
			$this->data['id'] = $this->uri->segment(3);
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = 1;		
			$this->data['page_title']=$this->_page_title;			
			$this->load->view('common/common_view_admin',$this->data);					
		}
	}
	
	//Controller: add
	function add()
	{
		$this->data["page_name"]="admin_newscate_add";
		$this->data['previous_id'] = 1;
		$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();
		$this->load->view('common/common_view_admin',$this->data);
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			//get submit data
			if ($_POST['cbo_news_cate'] != "")
				$this->submitdata['parent_id'] = $_POST['cbo_news_cate'];
			$this->submitdata['cate_name'] = $_POST['txt_cate_name'];
			$this->submitdata['url'] =  convertToVn(trim($_POST['txt_cate_name']));
			$this->submitdata['status'] =  $_POST['cbo_Status'];
			
			//insert newscate
			$insert_id = $this->Adminmodel->_insert_newscate('news_cate', $this->submitdata);	

			//write log	
			$this->Adminmodel->_write_log("Action,Step,Info", 
																		"insert news,insert DB,newsid=".$insert_id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
						
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã thêm mới ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_newscate/1'."'</script>";				
		}
	}
	
	//local func
	function index_get_news_cate_list()
	{
		$tmp1 = $this->Adminmodel->_get_news_cate_list();
		$tmp2 = $tmp1;
		$b = array();
		foreach($tmp1 as $key1=>$item1):
			if ($item1->parent_id=="")
			{
				$b[] = (object)array("parent_id"=>$item1->parent_id,
														 "parent_name"=>$item1->parent_name,
														 "cate_id"=>$item1->cate_id,
														 "cate_name"=>"&nbsp;&bull;&nbsp;".$item1->cate_name,
														 "url"=>$item1->url,
														 "status"=>$item1->status
														);		
				foreach($tmp2 as $key2=>$item2):
					if ($item1->cate_id == $item2->parent_id)
					{
						$b[] = (object)array("parent_id"=>$item2->parent_id,
																 "parent_name"=>$item2->parent_name,
																 "cate_id"=>$item2->cate_id,
																 "cate_name"=>"&nbsp;&bull;&nbsp;&bull;&nbsp;&bull;&nbsp;".$item2->cate_name,
																 "url"=>$item2->url,
																 "status"=>$item2->status
																);
					}
				endforeach; 
			}
		endforeach;
		return $b;
	}
	
	//local func
	function ddl_get_news_cate_list()
	{
		$tmp1 = $this->Adminmodel->_get_news_cate_list();
		$tmp2 = $tmp1;
		$b = array();
		foreach($tmp1 as $key1=>$item1):
			if ($item1->parent_id=="")
			{
				$b[] = (object)array("parent_id"=>$item1->parent_id,
														 "parent_name"=>$item1->parent_name,
														 "cate_id"=>$item1->cate_id,
														 "cate_name"=>"&nbsp;&bull;&nbsp;".$item1->cate_name,
														 "url"=>$item1->url,
														 "status"=>$item1->status
														);		
				foreach($tmp2 as $key2=>$item2):
					if ($item1->cate_id == $item2->parent_id)
					{
						$b[] = (object)array("parent_id"=>$item2->parent_id,
																 "parent_name"=>$item2->parent_name,
																 "cate_id"=>$item2->cate_id,
																 "cate_name"=>"&nbsp;&bull;&nbsp;&bull;&nbsp;&bull;&nbsp;".$item2->cate_name,
																 "url"=>$item2->url,
																 "status"=>$item2->status
																);
					}
				endforeach; 
			}
		endforeach;
		return $b;
	}	
	
	//local func: init data
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
		$this->_check_admin_login();
		
		//init data (this page, header admin)
		$this->_base_url = $this->config->item('base_url');
		$this->data['base_url'] = $this->config->item('base_url');
		$this->data['page_title'] = $this->_page_title;
		$this->data['admintitle_news'] = ADMINTITLE_NEWS;
                $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
                $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
                $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
		$this->data['admintitle_adv'] = ADMINTITLE_ADV;
		$this->data['admintitle_faq'] = ADMINTITLE_FAQ;
		$this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
		$this->data['admintitle_common'] = ADMINTITLE_COMMON;
		$this->data['admintitle_stats'] = ADMINTITLE_STATS;
		$this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
		$this->data['website_name'] = WEBSITE_NAME;		
		$this->data['link_page_name'] = $this->_page_name;
		$this->data['is_addnew'] = "add"; //show link add / list
		$this->data['page_id'] = $this->_page_id;	
		$this->data['page_name'] = $this->_page_name;
	}			
	
	//local func: check admin login
	function _check_admin_login()
	{
		if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
				(time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE
			 )
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
		else
			$this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT,time());
	}	

}
?>